package com.fair.concurrency.example.atomic;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/21 14:35
 */
@Slf4j
public class AtomicIntegerFieldUpdaterExample
{
    private static AtomicIntegerFieldUpdater updater =
            AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterExample.class,"count");

    @Getter
    public volatile int count = 100;
    private static AtomicIntegerFieldUpdaterExample example = new AtomicIntegerFieldUpdaterExample();
    public static void main(String[] args)
    {
        if(updater.compareAndSet(example,100,120)){
            log.info("update success 1,{}",example.getCount());
        }

        if(updater.compareAndSet(example,100,120)){
            log.info("update success 2,{}",example.getCount());
        }else {
            log.info("update failed,{}",example.getCount());
        }
    }
}
